//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "CCopyVersion.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
//---------------------------------------------------------------------------
//          CONSTRUCTEUR
//---------------------------------------------------------------------------
__fastcall TCopyVersion::TCopyVersion(TComponent* Owner)
    : TForm(Owner)
{
    dmWebHelp->tLogiciels->Active = true;
    dmWebHelp->tLogiciels->First();
    while(!dmWebHelp->tLogiciels->Eof)
    {
        cbLogiciel->Items->AddObject( astLogiciels(LOG_NOM), (TObject*) ntLogiciels(LOG_CODE) );
        dmWebHelp->tLogiciels->Next();
    }
}
//---------------------------------------------------------------------------
//          EVENEMENT COMBO BOX LOGICIEL
//---------------------------------------------------------------------------
void __fastcall TCopyVersion::cbLogicielChange(TObject *Sender)
{
    cbVerSource->Clear();
    TZZMySqlQuery * MaRequete = NULL;
    MaRequete = new TZZMySqlQuery( (TComponent*)0);
    MaRequete->Database = dmWebHelp->dbWebHelp;

    MaRequete->Sql->Text = (AnsiString) "SELECT ver_nom, ver_code FROM versions "+
                                        "left outer join logiciels on (versions.log_code = logiciels.log_code) "+
                                        "where versions.log_code = "+(int)cbLogiciel->Items->Objects[cbLogiciel->ItemIndex];

    MaRequete->Open();
    MaRequete->First();
    while(!MaRequete->Eof)
    {
        cbVerSource->Items->AddObject(  MaRequete->Fields->Fields[0]->AsString,
                                        (TObject*)MaRequete->Fields->Fields[1]->AsInteger);
        MaRequete->Next();
    }
    MaRequete->Sql->Clear();
    MaRequete->Close();
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
    cbVerSource->ItemIndex = 0;
}
//---------------------------------------------------------------------------
//          EVENEMENT COMBO BOX VERSION SOURCE
//--------------------------------------------------------------------------
void __fastcall TCopyVersion::cbVerSourceChange(TObject *Sender)
{
    int i = 0;
    int VersionSource = 0;
    int idx = cbVerSource->ItemIndex;
    
    cbVerCible->Clear();

    if(idx != AUCUNE_SELECTION) {
        VersionSource = (int) cbVerSource->Items->Objects[idx];
        for(i=0; i<cbVerSource->Items->Count; i++)
        {
            if(VersionSource != (int)cbVerSource->Items->Objects[i])
                cbVerCible->Items->AddObject(cbVerSource->Items->Strings[i],
                                             (TObject*)cbVerSource->Items->Objects[i]);
        }
        cbVerCible->ItemIndex = 0;
    }
}
//---------------------------------------------------------------------------
//          COPIER UNE VERSION
//---------------------------------------------------------------------------
void __fastcall TCopyVersion::bbOkClick(TObject *Sender)
{
    if(    cbLogiciel->ItemIndex    != AUCUNE_SELECTION
        && cbVerSource->ItemIndex   != AUCUNE_SELECTION
        && cbVerCible->ItemIndex    != AUCUNE_SELECTION) {

        //Recuperation d'un code logiciel
        //-------------------------------
        int CodeLogiciel = (int)cbLogiciel->Items->Objects[cbLogiciel->ItemIndex];

        //Recuperation d'un code version corespondant à l'aide source
        //-----------------------------------------------------------
        int CodeVersionSource = (int)cbVerSource->Items->Objects[cbVerSource->ItemIndex];

        //Recuperation d'un code version correspondant à l'aide cible
        //-----------------------------------------------------------
        int CodeVersionCible = (int)cbVerCible->Items->Objects[cbVerCible->ItemIndex];

        SupprimerRefCible(CodeLogiciel, CodeVersionCible);
        TZZMySqlQuery * MaRequete = NULL;
        MaRequete = new TZZMySqlQuery( (TComponent*)0);
        MaRequete->Database = dmWebHelp->dbWebHelp;

        MaRequete->Sql->Text = (AnsiString) "SELECT ref_nom,ref_sous_nom,ref_commentaire,ref_adresse FROM refer "
                                            "WHERE ref_code = "+
                                            "(SELECT ref_code FROM refer "+
                                            "WHERE log_code=\'"+CodeLogiciel+"\' AND ver_code=\'"+CodeVersionSource+"\' )";
        MaRequete->Open();
        MaRequete->First();

        dmWebHelp->tRefer->Active = true;
        dmWebHelp->tRefer->Insert();
        ntRefer(LOG_CODE)           = CodeLogiciel;
        ntRefer(VER_CODE)           = CodeVersionCible;
        astRefer(REF_NOM)           = MaRequete->Fields->Fields[0]->AsString;
        astRefer(REF_SOUS_NOM)      = MaRequete->Fields->Fields[1]->AsString;
        astRefer(REF_COMMENTAIRE)   = MaRequete->Fields->Fields[2]->AsString;
        astRefer(REF_ADRESSE)       = MaRequete->Fields->Fields[3]->AsString;
        dmWebHelp->tRefer->Post();
        dmWebHelp->trWebHelp->Commit();

        MaRequete->Sql->Clear();
        MaRequete->Close();
        if(MaRequete) {delete MaRequete; MaRequete = NULL;}
    }
    else {
        MessageDlg("Veuillez sélectionner tous les champs", mtWarning, TMsgDlgButtons() << mbOK, 0);
    }
}
//---------------------------------------------------------------------------
//              SUPPRIMER LA REFERENCE DE LA VERSION CIBLE
//---------------------------------------------------------------------------
void __fastcall TCopyVersion::SupprimerRefCible(const int Logiciel, const int RefCible)
{
    TZZMySqlQuery * MaRequete = NULL;
    MaRequete = new TZZMySqlQuery( (TComponent*)0);
    MaRequete->Database = dmWebHelp->dbWebHelp;

    MaRequete->Sql->Text = (AnsiString) "delete from refer "+
                                        "where log_code=\'"+Logiciel+"\' and ver_code=\'"+RefCible+"\'";
    MaRequete->ExecSql();
    MaRequete->Sql->Clear();

    MaRequete->Sql->Text = "Commit";
    MaRequete->ExecSql();
    MaRequete->Sql->Clear();

    MaRequete->Close();
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
}
